package com.zhdl.network.servertcp;

import com.project.command.instruction.service.data.InstructionValue;
import com.project.command.instruction.strategy.abs.AbsBeforeControlCommand;
import com.project.command.instruction.strategy.abs.BeforeStrategyParam;
import com.zhdl.network.modbusrtu.dto.ModbusDto;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

import static com.zhdl.network.servertcp.HandBeforeDefaultService.HAND_BEFORE_DEFAULT_SERVICE;

/**
 * 手操器默认返回数据
 *
 * @Author: zhangyy
 * @Email: zhang10092009@hotmail.com
 * @Date: 2023/4/18 11:07:20
 * @Version: v1.0
 * @Modified: 发送返回给手操器的数据，主要是状态和指标数据
 * @Description:
 */
@Slf4j
@Service(HAND_BEFORE_DEFAULT_SERVICE)
public class HandBeforeDefaultService extends AbsBeforeControlCommand<ModbusDto, ByteBuf> {
    public static final String HAND_BEFORE_DEFAULT_SERVICE = "handBeforeDefaultService";

    @Override
    public String getInstructionStr(BeforeStrategyParam param) {
        /**
         * 00 00 00 00 00 06 01 10 03 E8 00 05
         * 00 00 00 00
         * 00 06 MAP报文头，长度表示接下来的数据长度，不包含长度这一项
         * 01 地址域
         * 10 功能码
         * 03 E8 十进制1000，起始地址
         * 00 05 ，寄存器数量
         */
        //00 00 00 00 00 11 01 10 03 E8 00 05 0A 00 01 00 66 00 22 07 FA 00 01
        //00 00 00 00 00 11 01 10 03 E8 00 05 0A 00 01 00 66 00 23 07 FA 00 01
        //00 00 00 00 00 11 01 10 03 E8 00 05 0A 00 01 00 66 00 24 07 FA 00 01
        //00 00 00 00 00 11 01 10 03 E8 00 05 0A 00 01 00 66 00 25 07 FA 00 01
        String cmdStr = param.getValues().stream().map(InstructionValue::getContent).collect(Collectors.joining(""));
        // 01 10 03 E8 00 05
        return "0006" + cmdStr;
    }
}
